package com.hiof.moraa.server.controller.restfulservice;

import com.hiof.moraa.server.service.MapService;
import javax.servlet.http.HttpServletRequest;

import com.hiof.moraa.server.service.model.StartRouteStatus;
import com.hiof.moraa.server.service.model.StopRouteStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/restfulservice")
public class RouteServiceController {

    @Autowired
    MapService mapService;

    @RequestMapping(value = "/startroute", method = RequestMethod.POST)
    @ResponseBody
    public StartRouteStatus startRoute(
            @RequestParam("routeId") int routeId,
            HttpServletRequest request) {
        try {
            mapService.initializeMap(routeId, request.getSession().getServletContext());
            return StartRouteStatus.OK;
        } catch (Exception ex) {
            return StartRouteStatus.ServerError;
        }
    }

    @RequestMapping(value = "/stoproute", method = RequestMethod.POST)
    @ResponseBody
    public StopRouteStatus stoproute(
            @RequestParam("routeId") int routeId) {
        try {
            mapService.destroyMap(routeId);
            return StopRouteStatus.OK;
        } catch (Exception ex) {
            return StopRouteStatus.ServerError;
        }
    }

    @RequestMapping(value = "/runingroute", method = RequestMethod.GET)
    @ResponseBody
    public String getRunningRoute() {
        String result = "Running route id: ";
        for (Integer routeId : mapService.getRunningRouteId()) {
            result += routeId + " ";
        }
        return result;
    }
}
